package com.smile.mybatis;

import java.io.IOException;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;

public class UpdateTableIdAnnotation {

    public static void main(String[] args) {
        // 指定要扫描的文件夹路径
        String folderPath = "D:\\cfdp_java\\cpw-cfdp\\src\\main\\java\\com\\visense\\smart\\pharm\\cfdp\\domain\\cpw";

        try {
            processFolder(folderPath);
            System.out.println("处理完成！");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 处理文件夹中的所有Java文件
     * @param folderPath 文件夹路径
     * @throws IOException IO异常
     */
    public static void processFolder(String folderPath) throws IOException {
        Path startPath = Paths.get(folderPath);

        Files.walkFileTree(startPath, new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                if (file.toString().endsWith(".java")) {
                    processJavaFile(file);
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }

    /**
     * 处理单个Java文件，将@TableId中的"ID"改为"id"
     * @param filePath 文件路径
     * @throws IOException IO异常
     */
    public static void processJavaFile(Path filePath) throws IOException {
        // 读取文件内容
        List<String> lines = Files.readAllLines(filePath);
        List<String> newLines = new ArrayList<>();
        boolean modified = false;

        // 处理每一行
        for (String line : lines) {
            String trimmedLine = line.trim();

            // 检查是否包含@TableId注解且value="ID"
            if (trimmedLine.contains("@TableId") && trimmedLine.contains("value = \"ID\"")) {
                // 将"ID"替换为"id"
                String updatedLine = line.replace("value = \"ID\"", "value = \"id\"");
                newLines.add(updatedLine);
                modified = true;
            } else {
                newLines.add(line);
            }
        }

        // 如果文件被修改，则写回文件
        if (modified) {
            Files.write(filePath, newLines, StandardOpenOption.TRUNCATE_EXISTING);
            System.out.println("已处理文件: " + filePath);
        }
    }
}












